/**
 * 
 */
package jm.lib.web.jsf;

import java.util.Map;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author JimingLiu
 *
 */
public class JSFUtil {
	
	public static String getParameterValue(String name) {
		return getRequest().getParameter(name);
	}
	
	public static String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}
	
	public static Map getRequestParameterMap() {
		ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
		return ctx.getRequestParameterMap();
	}
	
	public static HttpServletRequest getRequest() {
		ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
		return (HttpServletRequest)ctx.getRequest();
	}
	
	public static HttpSession getSession() {
		ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
		return (HttpSession)ctx.getSession(false);
	}
	
	public static ServletContext getApplication() {
		ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
		return (ServletContext)ctx.getContext();
	}
	
	public static Object getObjectById(String objectId) {
		FacesContext ctx = FacesContext.getCurrentInstance();
		ValueBinding vb = ctx.getApplication().createValueBinding(
				"#{" + objectId + "}");
		return vb.getValue(ctx);
	}
	
	public static Object findAttribute(String name) {
		Object result = null;
		ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
		result = ctx.getRequestMap().get(name); 
		if(null == result) {
			result = ctx.getSessionMap().get(name); 
			if(null == result) {
				result = ctx.getApplicationMap().get(name); 
			}
		}
		return result;
	}
    
    public static Object eval(String expr) {
        FacesContext context = FacesContext.getCurrentInstance();
        ValueBinding binding = context.getApplication()
                .createValueBinding(expr);
        return binding.getValue(context);
    }

    public static void addErrorMessage(FacesContext context, String compId,
            String messageId) {
        ResourceBundle bundle = ResourceBundle.getBundle(context
                .getApplication().getMessageBundle());
        FacesMessage message = new FacesMessage(bundle.getString(messageId));
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        context.addMessage(compId, message);
    }

    public static void addExceptionMessage(Exception x) {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage message = new FacesMessage(x.getMessage());
        message.setSeverity(FacesMessage.SEVERITY_FATAL);
        context.addMessage(null, message);
    }
    
    /**
     * Add information message.
     *
     * @param msg the information message
     */
    public static void addInfoMessage(String msg) {
        addInfoMessage(null, msg);
    }

    /**
     * Add information message to a sepcific client.
     *
     * @param clientId the client id
     * @param msg the information message
     */
    public static void addInfoMessage(String clientId, String msg) {
		FacesContext.getCurrentInstance().addMessage(clientId,
				new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
	}

    /**
	 * Add error message.
	 * 
	 * @param msg
	 *            the error message
	 */
    public static void addErrorMessage(String msg) {
        addErrorMessage(null, msg);
    }

    /**
     * Add error message to a sepcific client.
     *
     * @param clientId the client id
     * @param msg the error message
     */
    public static void addErrorMessage(String clientId, String msg) {
		FacesContext.getCurrentInstance().addMessage(clientId,
				new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
	}

    
}
